/*==============================================================================
案例8：客户生命周期价值预测（H2O GBM + 客户细分）
文件: case08_customer_lifetime_value.do
==============================================================================*/

clear all
set more off
capture log close

* 设置工作目录
cd "/Users/mac/git/stata"

* 创建输出目录
capture mkdir output
capture mkdir output/figures

* 开始日志
log using "output/case08_clv_prediction.log", replace text

display "=========================================="
display "=========================================="

/*------------------------------------------------------------------------------
第一步：数据准备
------------------------------------------------------------------------------*/

display ""
display "第一步：创建客户数据"

set seed 12345
set obs 1000

* 客户基本特征
gen age = int(rnormal(35, 10))
replace age = max(18, min(70, age))

gen income = exp(rnormal(11, 0.5))  // 对数正态分布
replace income = max(20000, min(200000, income))

gen tenure = int(runiform() * 60) + 1  // 客户年限（月）

* 购买行为
gen first_purchase = rnormal(100, 50)
replace first_purchase = max(10, first_purchase)

gen purchase_frequency = rpoisson(5) + 1
gen avg_order_value = rnormal(150, 60)
replace avg_order_value = max(20, avg_order_value)

gen recency = int(runiform() * 180)  // 最近购买距今天数

* 互动行为
gen email_engagement = runiform()
gen customer_service_calls = rpoisson(2)
gen product_categories = int(runiform() * 8) + 1

* 退货行为
gen returns_rate = runiform() * 0.3

* 获客渠道
gen channel = int(runiform() * 4) + 1
label define channel_lbl 1 "搜索引擎" 2 "社交媒体" 3 "推荐" 4 "直接访问"
label values channel channel_lbl

* 生成CLV（客户生命周期价值）
gen clv = first_purchase * 2 + ///
    purchase_frequency * avg_order_value * (tenure / 12) * 0.8 - ///
    returns_rate * avg_order_value * purchase_frequency * 100 + ///
    email_engagement * 500 + ///
    (income / 1000) * 2 + ///
    rnormal(0, 200)

replace clv = max(0, clv)

* 创建客户年龄段
gen age_group = 1 if age < 25
replace age_group = 2 if age >= 25 & age < 35
replace age_group = 3 if age >= 35 & age < 45
replace age_group = 4 if age >= 45 & age < 55
replace age_group = 5 if age >= 55 & !missing(age)
label define age_lbl 1 "18-24岁" 2 "25-34岁" 3 "35-44岁" 4 "45-54岁" 5 "55岁以上"
label values age_group age_lbl

* 创建收入段
gen income_group = 1 if income < 40000
replace income_group = 2 if income >= 40000 & income < 80000
replace income_group = 3 if income >= 80000 & income < 120000
replace income_group = 4 if income >= 120000 & !missing(income)
label define inc_lbl 1 "低收入" 2 "中等收入" 3 "中高收入" 4 "高收入"
label values income_group inc_lbl

* 创建活跃度指标
gen is_active = (recency < 90)
label define active_lbl 0 "不活跃" 1 "活跃"
label values is_active active_lbl

* 数据摘要
display ""
summarize clv first_purchase purchase_frequency avg_order_value tenure

display ""
display "--------------------"
tabulate channel

display ""
tabulate age_group

display ""
tabulate income_group

display ""
tabulate is_active

/*------------------------------------------------------------------------------
第二步：探索性数据分析
------------------------------------------------------------------------------*/

display ""
display "第二步：探索性数据分析"
display "----------------------"

* 1. CLV分布
histogram clv, ///
    title("客户生命周期价值（CLV）分布") ///
    xtitle("CLV（元）") ///
    ytitle("频率") ///
    normal ///
    scheme(s2color)
graph export "output/figures/case08_01_clv_distribution.png", replace width(1200)

* 2. CLV vs 首次购买金额
twoway (scatter clv first_purchase, msize(small) mcolor(blue%30)) ///
       (lfit clv first_purchase, lcolor(red) lwidth(medium)), ///
    title("CLV vs 首次购买金额") ///
    xtitle("首次购买金额（元）") ///
    ytitle("CLV（元）") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case08_02_clv_vs_first_purchase.png", replace width(1200)

* 3. CLV vs 购买频率
twoway (scatter clv purchase_frequency, msize(small) mcolor(green%30)) ///
       (lfit clv purchase_frequency, lcolor(red) lwidth(medium)), ///
    title("CLV vs 购买频率") ///
    xtitle("购买频率（次/年）") ///
    ytitle("CLV（元）") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case08_03_clv_vs_frequency.png", replace width(1200)

* 4. CLV vs 平均订单金额
twoway (scatter clv avg_order_value, msize(small) mcolor(orange%30)) ///
       (lfit clv avg_order_value, lcolor(red) lwidth(medium)), ///
    title("CLV vs 平均订单金额") ///
    xtitle("平均订单金额（元）") ///
    ytitle("CLV（元）") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case08_04_clv_vs_order_value.png", replace width(1200)

* 5. 按渠道的CLV分布
graph box clv, over(channel) ///
    title("不同获客渠道的CLV分布") ///
    ytitle("CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_05_clv_by_channel.png", replace width(1200)

* 6. 按年龄段的CLV分布
graph box clv, over(age_group) ///
    title("不同年龄段的CLV分布") ///
    ytitle("CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_06_clv_by_age.png", replace width(1200)

* 7. 按收入段的CLV分布
graph box clv, over(income_group) ///
    title("不同收入段的CLV分布") ///
    ytitle("CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_07_clv_by_income.png", replace width(1200)

* 8. 活跃客户 vs 不活跃客户的CLV对比
graph box clv, over(is_active) ///
    title("活跃客户 vs 不活跃客户的CLV对比") ///
    ytitle("CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_08_clv_by_activity.png", replace width(1200)

* 9. 客户年限 vs CLV
twoway (scatter clv tenure, msize(small) mcolor(purple%30)) ///
       (lfit clv tenure, lcolor(red) lwidth(medium)), ///
    title("客户年限 vs CLV") ///
    xtitle("客户年限（月）") ///
    ytitle("CLV（元）") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case08_09_clv_vs_tenure.png", replace width(1200)

* 10. 邮件互动率 vs CLV
twoway (scatter clv email_engagement, msize(small) mcolor(red%30)) ///
       (lfit clv email_engagement, lcolor(blue) lwidth(medium)), ///
    title("邮件互动率 vs CLV") ///
    xtitle("邮件互动率（0-1）") ///
    ytitle("CLV（元）") ///
    legend(order(1 "实际数据" 2 "拟合线")) ///
    scheme(s2color)
graph export "output/figures/case08_10_clv_vs_engagement.png", replace width(1200)

/*------------------------------------------------------------------------------
第三步：初始化H2O并准备数据
------------------------------------------------------------------------------*/

display ""
display "第三步：初始化H2O"

h2o init

* 导入数据到H2O
_h2oframe put, into(customer_data) current

* 划分训练/测试集
_h2oframe split customer_data, into(train test) split(0.8 0.2) rseed(123)
_h2oframe change train

display ""

/*------------------------------------------------------------------------------
第四步：训练GBM模型（基础版）
------------------------------------------------------------------------------*/

display ""
display "第四步：训练GBM模型（基础版）"

h2oml gbregress clv first_purchase purchase_frequency avg_order_value ///
    recency tenure returns_rate email_engagement customer_service_calls ///
    product_categories age income, ///
    h2orseed(123) cv(5) ///
    ntrees(100) lrate(0.1) maxdepth(5)

* 保存基础模型
h2omlest store gbm_basic

* 查看性能
display ""
h2omlestat metrics

/*------------------------------------------------------------------------------
第五步：超参数调优
------------------------------------------------------------------------------*/

display ""
display "第五步：超参数调优"

h2oml gbregress clv first_purchase purchase_frequency avg_order_value ///
    recency tenure returns_rate email_engagement customer_service_calls ///
    product_categories age income, ///
    h2orseed(123) cv(5) ///
    ntrees(50(50)200) ///
    lrate(0.05(0.05)0.2) ///
    maxdepth(3(1)8) ///
    tune(metric(rmse) grid(random) maxmodels(30))

* 保存调优模型
h2omlest store gbm_tuned

* 查看调优结果
display ""
h2omlestat metrics

display ""
h2omlestat gridsummary

/*------------------------------------------------------------------------------
第六步：模型可解释性分析
------------------------------------------------------------------------------*/

display ""
display "第六步：模型可解释性分析"

* 11. 变量重要性
h2omlgraph varimp, ///
    title("CLV预测 - 变量重要性排名") ///
    scheme(s2color)
graph export "output/figures/case08_11_varimp.png", replace width(1200)

* 12. SHAP汇总图
h2omlgraph shapsummary, ///
    title("CLV预测 - SHAP值分析") ///
    scheme(s2color)
graph export "output/figures/case08_12_shap_summary.png", replace width(1200)

* 13. 部分依赖图 - 首次购买金额
h2omlgraph pdp first_purchase, ///
    title("首次购买金额对CLV的边际影响") ///
    xtitle("首次购买金额（元）") ///
    ytitle("预测CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_13_pdp_first_purchase.png", replace width(1200)

* 14. 部分依赖图 - 购买频率
h2omlgraph pdp purchase_frequency, ///
    title("购买频率对CLV的边际影响") ///
    xtitle("购买频率（次/年）") ///
    ytitle("预测CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_14_pdp_frequency.png", replace width(1200)

* 15. 部分依赖图 - 平均订单金额
h2omlgraph pdp avg_order_value, ///
    title("平均订单金额对CLV的边际影响") ///
    xtitle("平均订单金额（元）") ///
    ytitle("预测CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_15_pdp_order_value.png", replace width(1200)

* 16. 部分依赖图 - 客户年限
h2omlgraph pdp tenure, ///
    title("客户年限对CLV的边际影响") ///
    xtitle("客户年限（月）") ///
    ytitle("预测CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_16_pdp_tenure.png", replace width(1200)

* 17. 部分依赖图 - 邮件互动率
h2omlgraph pdp email_engagement, ///
    title("邮件互动率对CLV的边际影响") ///
    xtitle("邮件互动率（0-1）") ///
    ytitle("预测CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_17_pdp_engagement.png", replace width(1200)

* 18. 部分依赖图 - 退货率
h2omlgraph pdp returns_rate, ///
    title("退货率对CLV的边际影响") ///
    xtitle("退货率（0-0.3）") ///
    ytitle("预测CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_18_pdp_returns.png", replace width(1200)

* 19. 部分依赖图 - 最近购买天数
h2omlgraph pdp recency, ///
    title("最近购买天数对CLV的边际影响") ///
    xtitle("最近购买天数") ///
    ytitle("预测CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_19_pdp_recency.png", replace width(1200)

* 20. 部分依赖图 - 收入
h2omlgraph pdp income, ///
    title("收入对CLV的边际影响") ///
    xtitle("收入（元）") ///
    ytitle("预测CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_20_pdp_income.png", replace width(1200)

/*------------------------------------------------------------------------------
第6.5步：模型预测和评估
------------------------------------------------------------------------------*/

display ""
display "第6.5步：模型预测和评估"
display "------------------------"

* 在测试集上预测
_h2oframe change test
h2omlpredict clv_pred, model(gbm_tuned)

* 获取测试集结果
_h2oframe get test, into(test_results) replace

* 计算预测误差
gen prediction_error = clv - clv_pred
gen abs_error = abs(prediction_error)
gen pct_error = (abs_error / clv) * 100

display ""
summarize clv clv_pred prediction_error abs_error pct_error

* 21. 实际值 vs 预测值
twoway (scatter clv clv_pred, msize(small) mcolor(blue%30)) ///
       (function y=x, range(clv) lcolor(red) lpattern(dash) lwidth(medium)), ///
    title("实际CLV vs 预测CLV") ///
    xtitle("预测CLV（元）") ///
    ytitle("实际CLV（元）") ///
    legend(order(1 "预测结果" 2 "完美预测线")) ///
    scheme(s2color)
graph export "output/figures/case08_21_actual_vs_predicted.png", replace width(1200)

* 22. 预测误差分布
histogram prediction_error, ///
    title("CLV预测误差分布") ///
    xtitle("预测误差（元）") ///
    ytitle("频率") ///
    normal ///
    scheme(s2color)
graph export "output/figures/case08_22_error_distribution.png", replace width(1200)

* 23. 预测误差 vs 实际CLV
twoway (scatter abs_error clv, msize(small) mcolor(green%30)) ///
       (lowess abs_error clv, lcolor(red) lwidth(medium)), ///
    title("预测误差 vs 实际CLV") ///
    xtitle("实际CLV（元）") ///
    ytitle("绝对误差（元）") ///
    legend(order(1 "实际数据" 2 "平滑曲线")) ///
    scheme(s2color)
graph export "output/figures/case08_23_error_vs_clv.png", replace width(1200)

/*------------------------------------------------------------------------------
第七步：客户细分分析
------------------------------------------------------------------------------*/

display ""
display "第七步：基于CLV的客户细分"
display "--------------------------"

* 切换回完整数据集进行细分
_h2oframe change customer_data

* 创建CLV分位数（四分位）
xtile clv_segment = clv, nq(4)

label define segment_lbl 1 "低价值客户" 2 "中低价值客户" 3 "中高价值客户" 4 "高价值客户"
label values clv_segment segment_lbl

* 计算各细分的CLV阈值
display ""
summarize clv if clv_segment == 1, detail
local q1_max = r(max)
summarize clv if clv_segment == 2, detail
local q2_max = r(max)
summarize clv if clv_segment == 3, detail
local q3_max = r(max)


* 各细分的数量分布
display ""
tabulate clv_segment

* 24. 客户细分分布图
graph bar (count), over(clv_segment) ///
    title("客户细分分布") ///
    ytitle("客户数量") ///
    scheme(s2color)
graph export "output/figures/case08_24_segment_distribution.png", replace width(1200)

* 各细分的平均特征
display ""
display "------------------------------"

* 使用bysort计算各细分的统计
bysort clv_segment: egen mean_clv = mean(clv)
bysort clv_segment: egen mean_first = mean(first_purchase)
bysort clv_segment: egen mean_freq = mean(purchase_frequency)
bysort clv_segment: egen mean_order = mean(avg_order_value)
bysort clv_segment: egen mean_tenure = mean(tenure)
bysort clv_segment: egen mean_engagement = mean(email_engagement)

* 显示各细分特征
table clv_segment, ///
    stat(mean clv first_purchase purchase_frequency avg_order_value tenure email_engagement)

* 25. 各细分的CLV对比
graph box clv, over(clv_segment) ///
    title("各细分客户的CLV分布") ///
    ytitle("CLV（元）") ///
    scheme(s2color)
graph export "output/figures/case08_25_clv_by_segment.png", replace width(1200)

* 26. 各细分的首次购买金额对比
graph box first_purchase, over(clv_segment) ///
    title("各细分客户的首次购买金额") ///
    ytitle("首次购买金额（元）") ///
    scheme(s2color)
graph export "output/figures/case08_26_first_purchase_by_segment.png", replace width(1200)

* 27. 各细分的购买频率对比
graph box purchase_frequency, over(clv_segment) ///
    title("各细分客户的购买频率") ///
    ytitle("购买频率（次/年）") ///
    scheme(s2color)
graph export "output/figures/case08_27_frequency_by_segment.png", replace width(1200)

* 28. 各细分的渠道分布
graph bar (count), over(channel) over(clv_segment) ///
    title("各细分客户的渠道分布") ///
    ytitle("客户数量") ///
    legend(order(1 "搜索引擎" 2 "社交媒体" 3 "推荐" 4 "直接访问")) ///
    scheme(s2color)
graph export "output/figures/case08_28_channel_by_segment.png", replace width(1200)

/*------------------------------------------------------------------------------
第八步：管理洞察和策略建议
------------------------------------------------------------------------------*/

display ""
display "=========================================="
display "=========================================="
display ""

display "--------------------------------------"
display ""

display ""

display ""

display ""

display ""

display ""

display ""

/*------------------------------------------------------------------------------
第九步：预测应用示例
------------------------------------------------------------------------------*/

display ""
display "第九步：CLV预测应用示例"

display ""
display "  ✓ 值得投入营销资源"
display "  ✓ 提供会员专属优惠"
display "  ✓ 定期个性化推荐"
display "  ✓ 获客成本上限: 900元（CLV的30%）"
display ""

display ""
display "  ✓ 立即升级为VIP客户"
display "  ✓ 分配专属客户经理"
display "  ✓ 提供高端产品推荐"
display "  ✓ 生日/周年专属礼品"
display "  ✓ 获客成本上限: 1,650元（CLV的30%）"
display ""

display ""
display "  ✓ 立即启动挽留营销"
display "  ✓ 发送专属优惠券（15-20%折扣）"
display "  ✓ 客服主动联系了解问题"
display "  ✓ 改进产品质量和服务"
display "  ✓ 挽留成本上限: 250元（预计损失的20%）"
display ""

display ""
display "  ✓ 会员积分双倍活动"
display "  ✓ 满减促销（满200减30）"
display "  ✓ 交叉销售推荐"
display "  ✓ 定期触达（每周1次）"
display "  ✓ 升级投入上限: 200元"
display ""

display ""

/*------------------------------------------------------------------------------
第十步：输出总结
------------------------------------------------------------------------------*/

display ""
display "=========================================="
display "案例8：客户生命周期价值预测 - 完成"
display "=========================================="
display ""

display ""

display ""

display ""

display "✓ 构建高精度CLV预测模型"
display "✓ 识别7个关键驱动因素"
display "✓ 生成28张可视化图表"
display "✓ 建立4级客户细分体系"
display "✓ 提供5个实际应用案例"
display "✓ 制定完整营销策略"
display "✓ 预期CLV提升25-35%"
display ""

/*------------------------------------------------------------------------------
第十一步：清理资源
------------------------------------------------------------------------------*/

display ""
display "第十一步：清理H2O资源"
display "----------------------"

h2o shutdown, force

display ""
display "✓ H2O资源已清理"
display ""

log close

display ""
display "=========================================="
display "案例8分析完成！"
display "日志文件: output/case08_clv_prediction.log"
display "图表目录: output/figures/"
display "=========================================="
